package com.woniu.woniuticket.service.impl;

import com.woniu.woniuticket.dao.OrderMapper;
import com.woniu.woniuticket.entity.OrderForm;
import com.woniu.woniuticket.entity.School;
import com.woniu.woniuticket.dao.SchoolMapper;
import com.woniu.woniuticket.service.ISchoolService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author spirit
 * @since 2020-01-02
 */
@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements ISchoolService {

    @Autowired
    private SchoolMapper schoolMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public School firstqualification(String schoolName) {
        System.out.println(schoolName+"学校");

        return schoolMapper.firstqualification(schoolName);
    }

    @Override
    public Map getMoney() {

        List<OrderForm> caseHistoryList = orderMapper.selectList(null);

        // 找出 x轴 最大显示几号
        int maxDay = 0;
        for (OrderForm caseHistory : caseHistoryList) {
            Date admissionDate = caseHistory.getOrderTime();
            if (admissionDate != null) {
                // 表示的月份中的某一天。返回的值在 1 和 31 之间
                int date2 = admissionDate.getDate();
                if (date2 > maxDay) maxDay = date2;
            }
        }

        // x轴数据：天数
        // y轴数据：当天的人数
        // 统计当天有多少人，从 1号 到最近入院那个人的 那一天

        // treeMap 有序，key: day; value: amount
        Map<Integer, BigDecimal> treeMap = new TreeMap<>();

        for (int i = 1; i <= maxDay; i++) {
            treeMap.put(i, BigDecimal.valueOf(0.0));
        }

        for (OrderForm caseHistory : caseHistoryList) {
            Date admissionDate = caseHistory.getOrderTime();
            BigDecimal money = caseHistory.getTotelPayment();

            if (admissionDate != null) {
                int date2 = admissionDate.getDate();
                BigDecimal get = treeMap.get(date2);
                treeMap.put(date2, get.add(money));
            }
        }

        List<Object> daysArr = new ArrayList<>();
        List<Object> amountArr = new ArrayList<>();

        for (Integer key : treeMap.keySet()) {
            System.out.println("key = " + key + ", value = " + treeMap.get(key));
            daysArr.add(key);
            amountArr.add(treeMap.get(key));
        }
        Set<Integer> keySet = treeMap.keySet();
        System.out.println(keySet);

        Map<String, Object> resp = new HashMap<>();
        resp.put("days", daysArr);
        resp.put("amount", amountArr);

        return resp;
    }
}
